//
//  OrderInfoView.m
//  Rabbit
//
//  Created 阿帕 on 2019/12/17.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "OrderInfoView.h"
#import "CYWebViewViewController.h"
//#import <Photos/PHPhotoLibrary.h>
//#import <Photos/PHProjectChangeRequest.h>
#import <Photos/Photos.h>

@interface OrderInfoView ()

@property (strong , nonatomic)UILabel *danhaoLabel;
//@property (strong , nonatomic)UILabel *fahuoAddressLabel;
//@property (strong , nonatomic)UILabel *shouhuoAddressLabel;
@property (strong , nonatomic)UIView *qrImgBgView;

@property (assign , nonatomic)NSInteger oneSectionCount;
@property (assign , nonatomic)NSInteger twoSectionCount;
@property (strong , nonatomic)UIView    *adressBgView;
@property (strong , nonatomic)UIView    *itemBgView;
@property (strong , nonatomic)UIView    *loadQrView;
@property (strong , nonatomic)UIView    *unloadQrView;
@property(nonatomic, assign)BOOL         isShowloadQr;
@property(nonatomic, assign)BOOL         isShowUnloadQr;

//@property (strong , nonatomic)UIImageView *loadQrImage;

@end

@implementation OrderInfoView

static const NSInteger tf_Height = 35;

#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        _oneSectionCount = 0;
        _twoSectionCount = 0;
        
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    
    if ([_response[@"isQuote"] integerValue] == 0 && [[UserDefaultUtil valueForKey:Login_Branch][@"isTax"] integerValue] == 1) {
        //不允许
        _oneSectionCount = 10;
        _twoSectionCount = 2;
    }
    if ([_response[@"isQuote"] integerValue] == 0 && [[UserDefaultUtil valueForKey:Login_Branch][@"isTax"] integerValue] == 0) {
        _oneSectionCount = 9;
        _twoSectionCount = 2;
    }
    if ([_response[@"isQuote"] integerValue] == 1 && [[UserDefaultUtil valueForKey:Login_Branch][@"isTax"] integerValue] == 0) {
        _oneSectionCount = 9;
        _twoSectionCount = 1;
    }
    if ([_response[@"isQuote"] integerValue] == 1 && [[UserDefaultUtil valueForKey:Login_Branch][@"isTax"] integerValue] == 1) {
        _oneSectionCount = 10;
        _twoSectionCount = 1;
    }
    
    if ([_response[@"isInsure"] integerValue] == 1) {
        // 已投保
        _twoSectionCount += 1;
    }
    
    UIView *danhaoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, tf_Height)];
    danhaoBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:danhaoBgView];
    
    UIView *blueLine = [[UIView alloc] initWithFrame:CGRectMake(15, 10, 2, 12)];
    blueLine.backgroundColor = Theme_ColorValue;
    [danhaoBgView addSubview:blueLine];
    
    _danhaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, 300, tf_Height)];
    _danhaoLabel.text = @"货源单号：-";
    _danhaoLabel.font = font(16);
    _danhaoLabel.adjustsFontSizeToFitWidth = YES;
    blueLine.centerY = _danhaoLabel.centerY;
    [danhaoBgView addSubview:_danhaoLabel];
    
    NSInteger driverType = [[UserDefaultUtil valueForKey:DRIVER_TYPE] integerValue];
    
#pragma mark - 装货、卸货二维码区域
    
    if ([_response[@"qrCodeOnOff"] integerValue] == 1 && (driverType == 0 || driverType == 2)) {

        NSArray *loadUnloadQrArr = @[@"装货码", @"卸货码"];
        for (int i=0; i<loadUnloadQrArr.count; i++) {
            UIButton *loadQrButton = [UIButton buttonWithType:UIButtonTypeCustom];
            loadQrButton.tag = 500 + i;
            [loadQrButton setTitle:loadUnloadQrArr[i] forState:UIControlStateNormal];
            loadQrButton.titleLabel.font = font(14);
            [loadQrButton setBackgroundColor:[UIColor whiteColor]];
            [loadQrButton setTitleColor:Dark_TextColor forState:UIControlStateNormal];
            [loadQrButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            [loadQrButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
            [loadQrButton addTarget:self action:@selector(loadUnloadQrBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            loadQrButton.frame = CGRectMake(0, danhaoBgView.cy_bottom + 10 + 45 * i, ScreenW, 45);
            [self addSubview:loadQrButton];
            
            UIImageView *rArrow = [[UIImageView alloc] initWithFrame:CGRectMake(loadQrButton.cy_right-25, 17.5f, 6, 10)];
            rArrow.tag = 510 + i;
            rArrow.image = image(@"gcy_icon_my_rArrow");
            [loadQrButton addSubview:rArrow];
            
            if (i==0) {
                UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, loadQrButton.cy_height-1, ScreenW-30, 1)];
                line.backgroundColor = BackgroundColor;
                [loadQrButton addSubview:line];
            }
        }
        
        
//        UIButton *unloadQrButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        unloadQrButton.tag = 501;
//        [unloadQrButton setTitle:@"卸货码" forState:UIControlStateNormal];
//        unloadQrButton.titleLabel.font = font(14);
//        [unloadQrButton setBackgroundColor:[UIColor whiteColor]];
//        [unloadQrButton setTitleColor:Dark_TextColor forState:UIControlStateNormal];
//        [unloadQrButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
//        [unloadQrButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
//        [unloadQrButton addTarget:self action:@selector(loadUnloadQrBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        unloadQrButton.frame = CGRectMake(0, loadQrButton.cy_bottom, ScreenW, 45);
//        [self addSubview:unloadQrButton];
        
        NSArray *qrArr = @[_response[@"loadingQr"] == NULL ? @"" : _response[@"loadingQr"],
                           _response[@"unloadingQr"] == NULL ? @"" : _response[@"unloadingQr"]];
        
        _loadQrView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 120)];
        _loadQrView.hidden = YES;
        _loadQrView.backgroundColor = [UIColor clearColor];
        [self addSubview:_loadQrView];
        
        UIImageView *loadQqImage = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenW-100)/2, 10, 100, 100)];
        loadQqImage.userInteractionEnabled = YES;
        loadQqImage.image = image(@"gcy_icon_plaholder");
        [_loadQrView addSubview:loadQqImage];
        [loadQqImage sd_setImageWithURL:[NSURL URLWithString:[qrArr firstObject]] placeholderImage:image(@"gcy_icon_plaholder")];
        
        [loadQqImage nyl_addTapGes:^{

            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *changeAction = [UIAlertAction actionWithTitle:@"查看装货二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

                HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
                browser.isFullWidthForLandScape = YES;
                browser.isNeedLandscape = YES;
                browser.currentImageIndex = 0;
                browser.imageArray = @[[qrArr firstObject]];
                [browser show];

            }];
            UIAlertAction *checkAction = [UIAlertAction actionWithTitle:@"保存装货二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

                if ([DCSpeedy isBlankString:[qrArr firstObject]]) {
                    [XSInfoView showInfo:@"图片为空，无法保存" onView:self.window];
                    return;
                }
                [loadQqImage sd_setImageWithURL:[NSURL URLWithString:[qrArr firstObject]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    // 保存图片
//                    [self saveImageToPhotoAlbum:image];
                    UIButton *loadQrButton = [self viewWithTag:500];
                    [self loadImageFinished:image andButton:loadQrButton];
                }];

            }];

            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){

            }];
            
            [alertController addAction:changeAction];
            [alertController addAction:checkAction];
            [alertController addAction:cancelAction];

            [[DCSpeedy getcurrentTopViewController] presentViewController:alertController animated:YES completion:nil];
        }];

        
        _unloadQrView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 120)];
        _unloadQrView.hidden = YES;
        _unloadQrView.backgroundColor = [UIColor clearColor];
        [self addSubview:_unloadQrView];
        
        UIImageView *unloadQqImage = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenW-100)/2, 10, 100, 100)];
        unloadQqImage.userInteractionEnabled = YES;
        unloadQqImage.image = image(@"gcy_icon_plaholder");
        [_unloadQrView addSubview:unloadQqImage];
        [unloadQqImage sd_setImageWithURL:[NSURL URLWithString:[qrArr lastObject]] placeholderImage:image(@"gcy_icon_plaholder")];
        
        [unloadQqImage nyl_addTapGes:^{

            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *changeAction = [UIAlertAction actionWithTitle:@"查看卸货二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

                HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
                browser.isFullWidthForLandScape = YES;
                browser.isNeedLandscape = YES;
                browser.currentImageIndex = 0;
                browser.imageArray = @[[qrArr lastObject]];
                [browser show];

            }];
            UIAlertAction *checkAction = [UIAlertAction actionWithTitle:@"保存卸货二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

                if ([DCSpeedy isBlankString:[qrArr lastObject]]) {
                    [XSInfoView showInfo:@"图片为空，无法保存" onView:self.window];
                    return;
                }
                [loadQqImage sd_setImageWithURL:[NSURL URLWithString:[qrArr lastObject]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    // 保存图片
//                    [self saveImageToPhotoAlbum:image];
                    UIButton *unloadButton = [self viewWithTag:501];
                    [self loadImageFinished:image andButton:unloadButton];
                }];

            }];

            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){

            }];

            [alertController addAction:changeAction];
            [alertController addAction:checkAction];
            [alertController addAction:cancelAction];

            [[DCSpeedy getcurrentTopViewController] presentViewController:alertController animated:YES completion:nil];
        }];
        
//        CGFloat qrImgWidth = (ScreenW-120)/2;
//
//        _qrImgBgView = [[UIView alloc] initWithFrame:CGRectMake(0, danhaoBgView.cy_bottom + 10, ScreenW, qrImgWidth + 20)];
//        _qrImgBgView.backgroundColor = [UIColor whiteColor];
//        [self addSubview:_qrImgBgView];
        
        
//        for (int i=0; i<qrArr.count; i++) {
//            UIImageView *rqImage = [[UIImageView alloc] initWithFrame:CGRectMake(40 + (40 + qrImgWidth)*i, 10, qrImgWidth, qrImgWidth)];
//            rqImage.userInteractionEnabled = YES;
//            rqImage.image = image(@"gcy_icon_plaholder");
//            [_qrImgBgView addSubview:rqImage];
//            [rqImage sd_setImageWithURL:[NSURL URLWithString:qrArr[i]] placeholderImage:image(@"gcy_icon_plaholder")];
//
//            [rqImage nyl_addTapGes:^{
//
//                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//                UIAlertAction *changeAction = [UIAlertAction actionWithTitle:@"查看二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//
//                    HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
//                    browser.isFullWidthForLandScape = YES;
//                    browser.isNeedLandscape = YES;
//                    browser.currentImageIndex =0;
//                    browser.imageArray = @[qrArr[i]];
//                    [browser show];
//
//                }];
//                UIAlertAction *checkAction = [UIAlertAction actionWithTitle:@"保存二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//
//                    if ([DCSpeedy isBlankString:qrArr[i]]) {
//                        [XSInfoView showInfo:@"图片为空，无法保存" onView:self.window];
//                        return;
//                    }
//                    [rqImage sd_setImageWithURL:[NSURL URLWithString:qrArr[i]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//                        // 保存图片
//                        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//                    }];
//
//                }];
//
//                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
//
//                }];
//
//                [alertController addAction:changeAction];
//                [alertController addAction:checkAction];
//                [alertController addAction:cancelAction];
//
//                [[DCSpeedy getcurrentTopViewController] presentViewController:alertController animated:YES completion:nil];
//            }];
//        }
    }
    
    //    ------------
    _adressBgView = [[UIView alloc] initWithFrame:CGRectMake(0, _unloadQrView.cy_bottom + 10, ScreenW, 140)];
    _adressBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_adressBgView];
    
    if ([_response[@"qrCodeOnOff"] integerValue] == 1 && (driverType == 0 || driverType == 2)) {
        _adressBgView.cy_y = danhaoBgView.cy_bottom + 10 + 100;
    } else{
        _adressBgView.cy_y = danhaoBgView.cy_bottom + 10;
    }
    
    NSArray *icons = @[@"gcy_icon_main_zhuang", @"gcy_icon_main_xie"];
    NSArray *texts = @[@"发货人:", @"发货电话:"];
    for (int i=0; i<2; i++) {
        UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10 + 35*i, 20, 20)];
        icon.image = image(icons[i]);
        [_adressBgView addSubview:icon];
        
        //装货地  卸货地
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 35*i+5, ScreenW-55, 40)];
        valueLabel.tag = 10 + i;
        valueLabel.text = @"---";
        valueLabel.textColor = Dark_TextColor;
        valueLabel.font = font(14);
        valueLabel.numberOfLines = 2;
        [_adressBgView addSubview:valueLabel];
        
        icon.centerY = valueLabel.centerY;
        
        //发货人 电话
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 + ScreenW/2*i + 10, 80 + 15, 65, tf_Height)];
        textLabel.text = texts[i];
        textLabel.textColor = Light_TextColor;
        textLabel.font = font(14);
        textLabel.adjustsFontSizeToFitWidth = YES;
        if(i == 1){
            textLabel.hidden = YES;
        }
        [_adressBgView addSubview:textLabel];
        
        UILabel *valueLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(85+ScreenW/2*i, 80  + 15, 120, tf_Height)];
        valueLabel2.tag = 20 + i;
        valueLabel2.text = @"-";
        valueLabel2.textColor = Dark_TextColor;
        valueLabel2.font = font(14);
        if(i == 1){
            valueLabel2.hidden = YES;
        }
        [_adressBgView addSubview:valueLabel2];
    }
    
    //横线
    UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, 79, ScreenW-30, 1)];
    hLine.backgroundColor = BackgroundColor;
    [_adressBgView addSubview:hLine];
    
    //导航按钮
    UIButton *navButton = [UIButton buttonWithType:UIButtonTypeCustom];
    navButton.tag = 66;
    [navButton setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
    [navButton setTitle:@"查看路线 >" forState:UIControlStateNormal];
    navButton.titleLabel.font = font(15);
    navButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    navButton.frame = CGRectMake(ScreenW-150-15, 3*tf_Height - 10, 150, tf_Height);
    navButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [navButton addTarget:self action:@selector(btnTypeClick:) forControlEvents:UIControlEventTouchUpInside];
    [_adressBgView addSubview:navButton];
    
    //    gcy_icon_order_other@2x
    //具体信息表
    _itemBgView = [[UIView alloc] initWithFrame:CGRectMake(0, _adressBgView.cy_bottom+10, ScreenW, 13*tf_Height + 150)];
    _itemBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_itemBgView];
    
    _itemBgView.height = (_oneSectionCount + _twoSectionCount + 2) * tf_Height + 150;
    
    NSArray *headerImages = @[@"gcy_icon_order_huowu", @"gcy_icon_order_feiyong", @"gcy_icon_order_other"];
    NSArray *headerTitles = @[@"货物信息", @"费用信息", @"其他信息"];
    NSString *industryCode = _response[@"industryCode"] == NULL ? @"" : _response[@"industryCode"];
    if (![industryCode isNotBlank]) {
        _oneSectionCount = 9;
    }
    
    //头部
    for (int i=0; i<headerImages.count; i++) {
        UIImageView *icon = [[UIImageView alloc] initWithImage:image(headerImages[i])];
        [_itemBgView addSubview:icon];
        
        UILabel *headLabel = [[UILabel alloc] init];
        headLabel.text = headerTitles[i];
        headLabel.textColor = Dark_TextColor;
        headLabel.font = font(16);
        [_itemBgView addSubview:headLabel];
        
        if (i==0) {
            icon.frame = CGRectMake(15, 15.5f, 16.5f, 19);
            headLabel.frame = CGRectMake(36.5f, 0, 100, 50);
            
        } else if (i==1) {
            icon.frame = CGRectMake(15, 15.5f + _oneSectionCount*tf_Height + 50, 15.5f, 19);
            headLabel.frame = CGRectMake(35.5f, _oneSectionCount*tf_Height + 50, 100, 50);
            
        } else {
        
            icon.frame = CGRectMake(15, 15.5f + (_oneSectionCount + _twoSectionCount)*tf_Height + 50*2, 19, 19);
            headLabel.frame = CGRectMake(39, (_oneSectionCount + _twoSectionCount)*tf_Height + 50*2, 100, 50);
            
        }
    }
    
    //items 1  老的7个
    NSArray *itemTitles = [[NSArray alloc] init];
    NSArray *itemValues = [[NSArray alloc] init];
    if ([[UserDefaultUtil valueForKey:Login_Branch][@"isTax"] integerValue] == 1 && [industryCode isNotBlank]) {
        itemTitles = @[@"货物名称",@"货物所属行业", @"需求车数", @"未接车数", @"货物数量", @"未接数量", @"合理损耗", @"装车时间", @"卸车时间", @"运输距离"];
        itemValues = @[@"-", @"-", @"-", @"-", @"-", @"-", @"-", @"-", @"-", @"-"];
    } else {
        itemTitles = @[@"货物名称", @"需求车数", @"未接车数", @"货物数量", @"未接数量", @"合理损耗", @"装车时间", @"卸车时间", @"运输距离"];
        itemValues = @[@"-", @"-", @"-", @"-", @"-", @"-", @"-", @"-", @"-"];
    }
    for (int i=0; i<itemTitles.count; i++) {
        UILabel *leftLabel = [[UILabel alloc] init];
        leftLabel.text = itemTitles[i];
        leftLabel.textColor = Light_TextColor;
        leftLabel.font = font(14);
        leftLabel.frame = CGRectMake(15, 50 + tf_Height*i, 120, tf_Height);
        [_itemBgView addSubview:leftLabel];
        
        UILabel *rightLabel = [[UILabel alloc] init];
        rightLabel.text = itemValues[i];
        rightLabel.tag = 50 + i;
        rightLabel.textColor = Dark_TextColor;
        rightLabel.font = font(14);
        rightLabel.textAlignment = NSTextAlignmentRight;
        rightLabel.frame = CGRectMake(ScreenW - 215, 50 + tf_Height*i, 200, tf_Height);
        [_itemBgView addSubview:rightLabel];
        if ([itemTitles[i] isEqual:@"货物名称"] || [itemTitles[i] isEqual:@"合理损耗"]) { // 显示内容加宽
            rightLabel.numberOfLines = 2;
            rightLabel.frame = CGRectMake(ScreenW - 285, 50 + tf_Height*i, 270, 40);
        }
    }
    
    //items 2
    
    NSMutableArray *itemTwoTitles = [[NSMutableArray alloc] init];
    [itemTwoTitles addObject:@"货物单价"];
    if ([_response[@"isInsure"] integerValue] == 1) {
        //已投保
        [itemTwoTitles addObject:@"投保金额"];
    }
    if ([_response[@"isQuote"] integerValue] == 0 && [_response[@"fareInfoDisplay"] integerValue] == 1) {
        [itemTwoTitles addObject:@"运费单价"];
    }
//    NSArray *itemTwoTitles = @[@"货物单价", @"运费单价"];
    NSArray *itemTwoValues = @[@"-", @"-", @"-"];
    
    for (int i=0; i<itemTwoTitles.count; i++) {
        UILabel *leftLabel = [[UILabel alloc] init];
        leftLabel.text = itemTwoTitles[i];
        leftLabel.textColor = Light_TextColor;
        leftLabel.font = font(14);
        leftLabel.frame = CGRectMake(15, 100 + itemTitles.count*tf_Height + tf_Height*i, 120, tf_Height);
        [_itemBgView addSubview:leftLabel];
        
        UILabel *rightLabel = [[UILabel alloc] init];
        rightLabel.text = itemTwoValues[i];
        rightLabel.tag = 60 + i;
        rightLabel.textColor = Dark_TextColor;
        rightLabel.font = font(14);
        rightLabel.textAlignment = NSTextAlignmentRight;
        rightLabel.frame = CGRectMake(ScreenW - 165, 100 + itemTitles.count*tf_Height + tf_Height*i, 150, tf_Height);
        [_itemBgView addSubview:rightLabel];
    }
    
    //items 3
    NSArray *itemOtherTitles = @[@"用车类型", @"运输要求"];
    NSArray *itemOtherValues = @[@"-", @"-"];
    
    
    for (int i=0; i<itemOtherTitles.count; i++) {
        UILabel *leftLabel = [[UILabel alloc] init];
        leftLabel.text = itemOtherTitles[i];
        leftLabel.textColor = Light_TextColor;
        leftLabel.font = font(14);
        leftLabel.frame = CGRectMake(15, 150 + (_oneSectionCount + _twoSectionCount)*tf_Height + tf_Height*i, 120, tf_Height);
        [_itemBgView addSubview:leftLabel];
        
        UILabel *rightLabel = [[UILabel alloc] init];
        rightLabel.tag = 70+i;
        rightLabel.text = itemOtherValues[i];
        rightLabel.textColor = Dark_TextColor;
        rightLabel.font = font(14);
        rightLabel.textAlignment = NSTextAlignmentRight;
        rightLabel.frame = CGRectMake(ScreenW - 215, 150 + (_oneSectionCount + _twoSectionCount)*tf_Height + tf_Height*i, 200, tf_Height);
        [_itemBgView addSubview:rightLabel];
    }
}

#pragma mark - 二维码展开隐藏
- (void)loadUnloadQrBtnClick:(UIButton *)button {
    UIButton *loadBtn = [self viewWithTag:500];
    UIButton *unloadBtn = [self viewWithTag:501];
    UIImageView *loadArr= [self viewWithTag:510];
    UIImageView *unloadArr = [self viewWithTag:511];
    if (button.tag == 500) {
        //装货
        _unloadQrView.hidden = YES;
        _adressBgView.top = unloadBtn.bottom;
        _itemBgView.top = _adressBgView.bottom;
        unloadArr.transform = CGAffineTransformMakeRotation(M_PI/180);
        
        if (button.selected) {
            
            _loadQrView.hidden = YES;
            unloadBtn.top = button.bottom;
            _adressBgView.top = unloadBtn.bottom + 10;
            _itemBgView.top = _adressBgView.bottom;
            loadArr.transform = CGAffineTransformMakeRotation(M_PI/180);
            
        } else {
            _loadQrView.hidden = NO;
            _loadQrView.top = button.bottom;
            unloadBtn.top = _loadQrView.bottom;
            _adressBgView.top = unloadBtn.bottom + 10;
            _itemBgView.top = _adressBgView.bottom;
            loadArr.transform = CGAffineTransformMakeRotation(90 * M_PI/180);
        }
        
        button.selected = !button.selected;
        
    } else {
        //卸货
        _loadQrView.hidden = YES;
        button.top = loadBtn.bottom;
        _adressBgView.top = button.bottom;
        _itemBgView.top = _adressBgView.bottom;
        loadArr.transform = CGAffineTransformMakeRotation(M_PI/180);
        
        if (button.selected) {
            
            _unloadQrView.hidden = YES;
            _adressBgView.top = unloadBtn.bottom + 10;
            _itemBgView.top = _adressBgView.bottom;
            unloadArr.transform = CGAffineTransformMakeRotation(M_PI/180);
            
        } else {
            _unloadQrView.hidden = NO;
            _unloadQrView.top = button.bottom;
            _adressBgView.top = _unloadQrView.bottom + 10;
            _itemBgView.top = _adressBgView.bottom;
            unloadArr.transform = CGAffineTransformMakeRotation(90 * M_PI/180);
        }
        
        button.selected = !button.selected;
    }
}

- (void)setResponse:(NSDictionary *)response {
    
    _response = response;
    
    [self removeAllSubviews];
    
    [self setUpUI];
    
    _danhaoLabel.text = CJStringWithFormat(@"货源单号：%@",_response[@"mainOrderNumber"]);
    
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:_danhaoLabel.text];
    [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:Dark_TextColor range:[_danhaoLabel.text rangeOfString:@"货源单号："]];
    [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:Theme_ColorValue range:[_danhaoLabel.text rangeOfString:CJStringWithFormat(@"%@",_response[@"mainOrderNumber"])]];
    [_danhaoLabel setAttributedText:attString];
    
    NSArray *contentArr = @[
        CJStringWithFormat(@"%@",_response[@"shipperName"]),
        CJStringWithFormat(@"%@",_response[@"shipperPhone"])
    ];
    for (int i=0; i<contentArr.count; i++) {
        UILabel *label = [self viewWithTag:20+i];
        label.text = contentArr[i];
    }
    
    NSArray *addressArr = @[
        CJStringWithFormat(@"%@%@", _response[@"shipperAddress"], _response[@"shipperDetailAddress"]),
        CJStringWithFormat(@"%@%@",_response[@"consigneeAddress"], _response[@"consigneeDetailAddress"])
    ];
    for (int i=0; i<addressArr.count; i++) {
        UILabel *label = [self viewWithTag:10+i];
        label.text = addressArr[i];
    }
    
#pragma mark - 列表信息展示
    //货物信息
    NSString *coalNumber = @"";
    NSString *notReceivedCount = @"";
    NSString *limitWastageAmount = @"";
    
    if ([_response[@"coalNumber"] integerValue] == 0) {
        coalNumber = @"不限";
    } else{
        coalNumber = CJStringWithFormat(@"%@车",_response[@"coalNumber"]);
    }
    
    if ([_response[@"notReceivedCount"] integerValue] == 0) {
        notReceivedCount = @"不限";
    } else {
        notReceivedCount = CJStringWithFormat(@"%@车",_response[@"notReceivedCount"]);
    }
    
    if ([_response[@"limitWastageAmount"] integerValue] == 0) {
        limitWastageAmount = @"不计";
    } else {
        limitWastageAmount = CJStringWithFormat(@"%@%@",_response[@"limitWastageAmount"], _response[@"limitWastageTypeName"]);//合理损耗
    }
    
    
    NSArray *array = [[NSArray alloc] init];
    NSString *industryCode = _response[@"industryCode"] == NULL ? @"" : _response[@"industryCode"];
    if ([[UserDefaultUtil valueForKey:Login_Branch][@"isTax"] integerValue] == 1 && [industryCode isNotBlank]) {
        
        array = @[
            CJStringWithFormat(@"%@",_response[@"coalTypeName"]),
            [NSString nullToString:CJStringWithFormat(@"%@",_response[@"industryName"])],
            coalNumber,
            notReceivedCount,
            [NSString stringWithFormat:@"%@%@",[DCSpeedy isPureInt:CJStringWithFormat(@"%@", _response[@"weight"])] ?  _response[@"weight"] : CJStringWithFormat(@"%.3f",  [_response[@"weight"] floatValue]) , _response[@"coalUnitName"]],
            
            [NSString stringWithFormat:@"%@%@",[DCSpeedy isPureInt:CJStringWithFormat(@"%@", _response[@"lastWeight"])] ?  _response[@"lastWeight"] : CJStringWithFormat(@"%.3f",  [_response[@"lastWeight"] floatValue]) , _response[@"coalUnitName"]],
            
            limitWastageAmount,
            CJStringWithFormat(@"%@",_response[@"lastLoadingTime"]),
            CJStringWithFormat(@"%@",_response[@"lastUnloadingTime"]),
            [NSString stringWithFormat:@"%@公里", [NSString nullToString:CJStringWithFormat(@"%.2f", [_response[@"distance"] floatValue])]],
        ];
        
    } else {
        array = @[
            CJStringWithFormat(@"%@",_response[@"coalTypeName"]),
            coalNumber,
            notReceivedCount,
//            CJStringWithFormat(@"%.3f%@",[_response[@"weight"] floatValue],_response[@"coalUnitName"]),
//            CJStringWithFormat(@"%.3f%@",[_response[@"lastWeight"] floatValue],_response[@"coalUnitName"]),
            
            [NSString stringWithFormat:@"%@%@",[DCSpeedy isPureInt:CJStringWithFormat(@"%@", _response[@"weight"])] ?  _response[@"weight"] : CJStringWithFormat(@"%.3f",  [_response[@"weight"] floatValue]) , _response[@"coalUnitName"]],
            
            [NSString stringWithFormat:@"%@%@",[DCSpeedy isPureInt:CJStringWithFormat(@"%@", _response[@"lastWeight"])] ?  _response[@"lastWeight"] : CJStringWithFormat(@"%.3f",  [_response[@"lastWeight"] floatValue]) , _response[@"coalUnitName"]],
            
            limitWastageAmount,
            CJStringWithFormat(@"%@",_response[@"lastLoadingTime"]),
            CJStringWithFormat(@"%@",_response[@"lastUnloadingTime"]),
            [NSString stringWithFormat:@"%@公里", [NSString nullToString:CJStringWithFormat(@"%.2f", [_response[@"distance"] floatValue])]],
        ];
    }
    for (int i=0; i<array.count; i++) {
        UILabel *label = [self viewWithTag:50+i];
        label.text = array[i];
    }
    
    //费用信息
    if ([_response[@"isQuote"] integerValue] == 1) {
        
        NSMutableArray *array2 = @[
            CJStringWithFormat(@"%.2f元/%@", [_response[@"coalPrice"] floatValue], _response[@"coalUnitName"]),//货物单价
        ].mutableCopy;
        if ([_response[@"isInsure"] integerValue] == 1) {
            [array2 addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元/车", [_response[@"insuranceFee"] floatValue])]];
        }
        
        for (int i=0; i<array2.count; i++) {
            UILabel *xxfLabel = [self viewWithTag:60+i];
            xxfLabel.text = array2[i];
        }
        
    } else {
        
        NSMutableArray *array2 = @[
            CJStringWithFormat(@"%.2f元/%@", [_response[@"coalPrice"] floatValue], _response[@"coalUnitName"]),//货物单价
            CJStringWithFormat(@"%.2f%@", [_response[@"realPrice"] floatValue], _response[@"isTrunkName"])//运费单价
        ].mutableCopy;
        if ([_response[@"isInsure"] integerValue] == 1) {
            [array2 insertObject:[NSString nullToString:CJStringWithFormat(@"%.2f元/车", [_response[@"insuranceFee"] floatValue])] atIndex:1];
        }
        
        for (int i=0; i<array2.count; i++) {
            UILabel *xxfLabel = [self viewWithTag:60+i];
            xxfLabel.text = array2[i];
        }
    }
    
    
    
    //其他信息
    NSArray *array3 = @[
        [DCSpeedy isBlankString:CJStringWithFormat(@"%@",response[@"pushVehicleUse"])] == YES ? @"暂无" : CJStringWithFormat(@"%@",response[@"pushVehicleUse"]),
        [DCSpeedy isBlankString:CJStringWithFormat(@"%@",response[@"coalName"])] == YES ? @"暂无" : CJStringWithFormat(@"%@",response[@"coalName"]),
    ];
    for (int i=0; i<array3.count; i++) {
        UILabel *label = [self viewWithTag:70+i];
        label.text = array3[i];
    }
}

- (void)btnTypeClick:(UIButton *)button {
    
    if (button.tag == 66) {
        
        NSString *shipperLongitude = [NSString nullToString:_response[@"shipperLongitude"]];//装货地经度
        NSString *shipperLatitude = [NSString nullToString:_response[@"shipperLatitude"]];//装货地纬度
        NSString *shipperAddress = [NSString nullToString:_response[@"shipperAddress"]];//装货地地址
        
        NSString *consigneeLongitude = [NSString nullToString:_response[@"consigneeLongitude"]];
        NSString *consigneeLatitude = [NSString nullToString:_response[@"consigneeLatitude"]];
        NSString *consigneeAddress = [NSString nullToString:_response[@"consigneeAddress"]];
        
        NSDictionary *dic = @{
            @"shipperLongitude": shipperLongitude,
            @"shipperLatitude": shipperLatitude,
            @"shipperAddress": shipperAddress,
            @"consigneeLongitude": consigneeLongitude,
            @"consigneeLatitude": consigneeLatitude,
            @"consigneeAddress": consigneeAddress,
        };
//        enclosure?orderCode=6118856b3a164340bcfd83eec5086a0c
        UIViewController *vc = [DCSpeedy getcurrentTopViewController];
        CYWebViewViewController *webVC = [[CYWebViewViewController alloc] init];
        webVC.titleString = @"查看路线";
        webVC.urlString = CJStringWithFormat(@"%@/admin/#/enclosure?orderCode=%@", BasicDomainURL, _response[@"code"]);
        webVC.state = 1;
        webVC.navDic = dic;
        [vc.navigationController pushViewController:webVC animated:YES];
    }
}

#pragma mark - 保存至相册
- (void)saveImageToPhotoAlbum:(UIImage*)savedImage{
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

//指定回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [XSInfoView showInfo:@"图片保存失败" onView:self.window];
    } else {
        [XSInfoView showInfo:@"图片保存成功" onView:self.window];
    }
}

/*
 此方式是iOS8之后加入的，他的使用稍微复杂一点，但是它允许进行批量的操作，例如添加、修改、删除等。如果要做更加复杂的操作的话，这种方式是比较推荐的方式
 */
- (void)loadImageFinished:(UIImage *)image andButton:(UIButton *)button {
    NSMutableArray *imageIds = [NSMutableArray array];
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        //写入图片到相册
        PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        //记录本地标识，等待完成后取到相册中的图片对象
        [imageIds addObject:req.placeholderForCreatedAsset.localIdentifier];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        DebugLog(@"success = %d, error = %@", success, error);
        if (success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [XSInfoView showInfo:@"图片保存成功" onView:self.window];
                
//                [self loadUnloadQrBtnClick:button];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [XSInfoView showInfo:@"图片保存失败" onView:self.window];
            });
        }
    }];
}

@end
